package cn.iocoder.yudao.module.ai.service.datasetmark;

import cn.iocoder.yudao.module.ai.dal.dataobject.datasetmark.DatasetLableFilecountDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.datasetmark.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.datasetmark.DatasetMarkDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.datasetmark.DatasetMarkMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * 数据集标注行 Service 实现类
 *
 * @author 侯俊青
 */
@Service
@Validated
public class DatasetMarkServiceImpl implements DatasetMarkService {

    @Resource
    private DatasetMarkMapper datasetMarkMapper;

    @Override
    public Long createDatasetMark(DatasetMarkSaveReqVO createReqVO) {
        // 插入
        DatasetMarkDO datasetMark = BeanUtils.toBean(createReqVO, DatasetMarkDO.class);
        datasetMarkMapper.insert(datasetMark);
        // 返回
        return datasetMark.getId();
    }

    @Override
    public void updateDatasetMark(DatasetMarkSaveReqVO updateReqVO) {
        // 校验存在
        validateDatasetMarkExists(updateReqVO.getId());
        // 更新
        DatasetMarkDO updateObj = BeanUtils.toBean(updateReqVO, DatasetMarkDO.class);
        datasetMarkMapper.updateById(updateObj);
    }

    @Override
    public void deleteDatasetMark(Long id) {
        // 校验存在
        validateDatasetMarkExists(id);
        // 删除
        datasetMarkMapper.deleteById(id);
    }

    private void validateDatasetMarkExists(Long id) {
        if (datasetMarkMapper.selectById(id) == null) {
            throw exception(DATASET_MARK_NOT_EXISTS);
        }
    }

    @Override
    public DatasetMarkDO getDatasetMark(Long id) {
        return datasetMarkMapper.selectById(id);
    }

    @Override
    public PageResult<DatasetMarkDO> getDatasetMarkPage(DatasetMarkPageReqVO pageReqVO) {
        return datasetMarkMapper.selectPage(pageReqVO);
    }

    @Override
    public List<DatasetMarkDO> getMarkListByMarkFileId(Long markFileId) {
        List<DatasetMarkDO> markDOList = datasetMarkMapper.selectList(DatasetMarkDO::getMarkFileId, markFileId);
        if(CollectionUtils.isNotEmpty(markDOList)){
            markDOList.sort(Comparator.comparingInt(DatasetMarkDO::getSortNum));
        }
        return markDOList;
    }

    @Override
    public List<DatasetMarkDO> getMarkListByDatasetId(Long datasetId) {
        List<DatasetMarkDO> markDOList = datasetMarkMapper.selectList(DatasetMarkDO::getDatasetId, datasetId);
        if(CollectionUtils.isNotEmpty(markDOList)){
            markDOList.sort(Comparator.comparingInt(DatasetMarkDO::getSortNum));
        }
        return markDOList;
    }

    @Override
    public List<DatasetMarkDO> getMarkListByOriginalFileId(Long originalFileId) {
        List<DatasetMarkDO> markDOList = datasetMarkMapper.selectList(DatasetMarkDO::getMarkOriginalFileId, originalFileId);
        if(CollectionUtils.isNotEmpty(markDOList)){
            markDOList.sort(Comparator.comparingInt(DatasetMarkDO::getSortNum));
        }
        return markDOList;
    }

    @Override
    public List<DatasetMarkDO> getMarkListByOriginalFileIdList(List<Long> originalFileIdList) {
        LambdaQueryWrapper<DatasetMarkDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(DatasetMarkDO::getMarkOriginalFileId, originalFileIdList);
        queryWrapper.orderByAsc(DatasetMarkDO::getMarkOriginalFileId,DatasetMarkDO::getSortNum);
        return datasetMarkMapper.selectList(queryWrapper);
    }

    @Override
    public void deleteByDatasetId(Long datasetId) {
        datasetMarkMapper.delete(DatasetMarkDO::getDatasetId, datasetId);
    }

    @Override
    public List<DatasetLableFilecountDO> groupFileCountByLabelId(Long datasetId) {
        return datasetMarkMapper.groupFileCountByLabelId(datasetId);
    }
}